package com.hw.dagl.controller;


import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hw.dagl.model.DepartmentNode;
import com.hw.dagl.model.Frm_Department;
import com.hw.dagl.model.Sys_User;
import com.hw.dagl.service.DepartmentService;
import com.hw.dao.utils.ControllerUtil;
import com.hw.dao.utils.Page;



@Controller
public class DepartmentController extends BaseController{
	@Resource
	private DepartmentService departmentServiceImpl;
	
	
	/**
	 * 跳转部门管理页面
	 * @param response
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String jybmHome(HttpServletResponse response){
		return "department/jybmHome";
	}
	
	
	/**
	 * 根据用户编号查询部门信息
	 * @param response
	 * @param info
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findDepartmentByGlbm(HttpServletResponse response,Frm_Department info){
		Frm_Department list = departmentServiceImpl.findDepartmentByGlbm(info);
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	
	
	/**
	 * 分页查询所有部门
	 * @param request
	 * @param response
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findAllBmPage(HttpServletRequest request,HttpServletResponse response,Page<Frm_Department> pages,Frm_Department info) {
		/*Sys_User user = (Sys_User) request.getSession().getAttribute("user");//得到当前登录角色
		String userSsbm = user.getGlbm();*/
		Page<Frm_Department> list=departmentServiceImpl.findDepartment(pages,info);
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	
	/**
	 * 批量删除部门信息
	 * @param response
	 * @param ids 用户帐号
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void delDepartment(HttpServletResponse response,String ids){
		boolean flag = departmentServiceImpl.delDepartment(ids);
		ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
	}
	
	
	/**
	 *	保存部门信息
	 * @param request
	 * @param response
	 * @param info
	 */
	@RequestMapping(method=RequestMethod.POST)	
	public void saveDepartment(HttpServletRequest request,HttpServletResponse response,Frm_Department info,String saveLx){
		if(!saveLx.equals("")){
			//修改
			boolean flag=departmentServiceImpl.updateDepartment(info);
			ControllerUtil.renderJson(response, flag, "encoding:UTF-8", "no-cache:false");
		}else{
			//新增
			boolean flag=departmentServiceImpl.saveDepartment(info);
			ControllerUtil.renderJson(response, flag, "encoding:UTF-8", "no-cache:false");
		}
		
	}
	
	
	/**
	 * 显示树图
	 * @param request
	 * @param response
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void queryDepartmentTree(HttpServletRequest request,HttpServletResponse response){
		Sys_User user = (Sys_User) request.getSession().getAttribute("user");//得到当前登录角色
		String glbm = null;
		if(user.getGlbm()==null){
			glbm = "340000000000";  
		}else{
			glbm = user.getGlbm();
		}
		List<Frm_Department> bmlist = departmentServiceImpl.queryDepartmentInfo(glbm);
		List<DepartmentNode> newList = new ArrayList<DepartmentNode>();
		DepartmentNode n = null;
		if(bmlist!=null && bmlist.size()>0){
			for (Frm_Department t : bmlist) {
				if(glbm.equals(t.getGlbm())){
					ArrayList<DepartmentNode> childlist = new ArrayList<DepartmentNode>();
					n = new DepartmentNode(t.getGlbm(),t.getBmmc(),t.getSjbm(),childlist,"","","");
				}
			}
		}
		ConvertNodeForDepartment(glbm,bmlist,n.getChild());
		newList.add(n);
		ControllerUtil.renderJson(response, newList,  "encoding:UTF-8", "no-cache:false");
	}
	/**
	 * 递归查找部门表
	 * @param parentId
	 * @param bmlist
	 * @param nodeList
	 */
	private void ConvertNodeForDepartment(String parentId, List<Frm_Department> bmlist,List<DepartmentNode> nodeList) {
		for (Frm_Department t : bmlist) {
			if(parentId.equals(t.getSjbm())) {
				ArrayList<DepartmentNode> childlist = new ArrayList<DepartmentNode>();
				DepartmentNode n = new DepartmentNode(t.getGlbm(),t.getBmmc(),t.getSjbm(),childlist,"","","");
				ConvertNodeForDepartment(n.getNodeId(),bmlist,n.getChild());
				nodeList.add(n);
			}
		}
	}
}
